/* 
 * Shuriken is the legal property of its developers. Please refer to the
 * COPYRIGHT file distributed with this
 * source distribution.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

using System;
using System.Collections.Generic;
using System.Text;
using Shuriken.Core.UI;
using Shuriken.Core.Store; 

namespace Shuriken.Core.UI.StandardUI
{
    class SubjectItemController : BaseItemController 
    {
        bool m_IsTextEntry;
        /// <summary>
        /// UI Slot for representing the subject
        /// </summary>
        /// <param name="ParentForm">Parent form</param>
        /// <param name="UIControl">UI control representing the subject</param>
        public SubjectItemController(frmMain ParentForm, InputItemView UIControl)
            : base(ParentForm, UIControl)
        {
            UIControl.SearchTerm = "Subject";
            m_IsTextEntry = false; 
        }

        protected override void ChangeSelectedItem(int NewIndex)
        {            
            Core.Store.ISearchable oldSubject = SelectedDisplayItem;

            base.ChangeSelectedItem(NewIndex);

            System.Diagnostics.Debug.Assert(SelectedDisplayItem != null, "Subject selection cannot be null!");
            Core.Store.ISearchable newSubject = SelectedDisplayItem;

            // check if null 
            if((m_ParentForm.ActionSlot.SelectedDisplayItem == null) || 
               (oldSubject != newSubject))
            {
                m_ParentForm.ActionSlot.LoadActionsForSubject((Shuriken.Base.ISubject)newSubject);
            }
        }

        /// <summary>
        /// Gets the list of initial display items.
        /// </summary>
        public override SortedDisplayItemList InitialDisplayItems
        {
            get
            {
                return new SortedDisplayItemList(); 
            }
        }

        /// <summary>
        /// Decides if tab is possible from this UI slot. 
        /// </summary>
        /// <returns>true if the next TAB is possible</returns>
        protected override bool TABPossible()
        {
            Shuriken.Base.ISubject subject = (Shuriken.Base.ISubject)SelectedDisplayItem;

            if (subject == null)
            {
                return false; 
            }

            return (subject.GetListOfActions().Count > 0);
        }

        /// <summary>
        /// Switching the controller to normal mode 
        /// </summary>
        internal void DisableTextEntryMode()
        {
            m_InputPanel.TextEntryMode = false;

            QNStoreItem si = (QNStoreItem)SelectedDisplayItem;
            if (si.InnerSubject.GetType() == typeof(QNTextSubject))
            {
                QNTextSubject text = (QNTextSubject)si.InnerSubject;
                text.Text = m_InputPanel.TextEntryText; 
            }

            m_InputPanel.ShowDisplayItem(SelectedDisplayItem, SearchKey, 0);
        }

        /// <summary>
        /// Switching the controller to text entry mode
        /// </summary>
        internal void EnableTextEntryMode()
        {
            if (SelectedDisplayItem == null || !(SelectedDisplayItem is QNStoreItem))
            {
                QNTextSubject text = new QNTextSubject();
                SelectedDisplayItem = new QNStoreItem(text);

                // Clearing the previous item
                SearchKey = string.Empty;

                if (DisplayItems != null)
                {
                    DisplayItems.Clear();
                }    
            }

            QNStoreItem si = (QNStoreItem)SelectedDisplayItem;
            if (si.InnerSubject.GetType() == typeof(QNTextSubject))
            {
                QNTextSubject text = (QNTextSubject)si.InnerSubject;
                m_InputPanel.TextEntryText = text.Text;
            }
            else
            {
                QNTextSubject text = new QNTextSubject();
                SelectedDisplayItem = new QNStoreItem(text);

                // Clearing the previous item
                SearchKey = string.Empty;

                if (DisplayItems != null)
                {
                    DisplayItems.Clear();
                }    
            }

            m_InputPanel.ShowDisplayItem(SelectedDisplayItem, SearchKey, 0);
            m_ParentForm.ActionSlot.LoadActionsForSubject((QNStoreItem)SelectedDisplayItem);

            m_IsTextEntry = true;
            m_InputPanel.TextEntryMode = true;
        }
    }
}
